package com.westa.smallshop.util.volley;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.westa.smallshop.data.PageData;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.westa.smallshop.util.Global.CHARACTER_ENCODING;

/**
 * Created by dune on 15/12/14.
 */
public class PageRequest<T> extends Request<PageData> {

    private boolean isHasToken = true;
    private final Class<T> mClazz;
    private final Response.Listener<List<T>> mListener;
    private final Map<String, String> mHeaders = new HashMap<>();
    private final Map<String, String> mParams = new HashMap<>();

    public PageRequest(String url, Class<T> clazz, Response.Listener<List<T>> listener) {

        this(Method.GET, url, clazz, listener);
    }

    public PageRequest(String url, Class<T> clazz, Response.Listener<List<T>> listener, Response.ErrorListener errorListener) {

        this(Method.GET, url, clazz, listener, errorListener);
    }

    public PageRequest(int method, String url, Class<T> clazz, Response.Listener<List<T>> listener) {
        this(method, url, clazz, listener, null);
    }

    public PageRequest(int method, String url, Class<T> clazz,
                       Response.Listener<List<T>> listener, Response.ErrorListener errorListener) {

        super(method, url, errorListener);
        this.mClazz = clazz;
        this.mListener = listener;
    }

    public void addHeader(String key, String value) {

        mHeaders.put(key, value);
    }

    public void addParam(String key, String value) {

        if (value != null) {
            mParams.put(key, value);
        }
    }

    public void addParam(String key, int value) {

        if (value != 0) {
            mParams.put(key, String.valueOf(value));
        }
    }

    public void addParams(Map<String, String> params) {

        mParams.putAll(params);
    }

    public void setHasToken(boolean hasToken) {
        isHasToken = hasToken;
    }

    @Override
    public String getUrl() {

        String url = MyHttpClient.createHttpUrl(mParams, super.getUrl(), getMethod());
        return TextUtils.isEmpty(url) ? super.getUrl() : url;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {

        mHeaders.putAll(MyHttpClient.getHeaders(isHasToken));
        return mHeaders;
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {

        return mParams;
    }

    @Override
    protected void deliverResponse(PageData response) {
        mListener.onResponse(JSONObject.parseArray(response.getResults(), mClazz));
    }

    @Override
    protected Response<PageData> parseNetworkResponse(NetworkResponse response) {
        try {
            String json = new String(response.data, CHARACTER_ENCODING);
            return Response.success(JSON.parseObject(json, PageData.class),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        }
    }
}
